/*
 * Copyright 2013 HVTTGROUP
 * http://hvttgroup.com
 *
 */
package com.hvtt.web.controllerUtils;

import com.hvtt.web.controller.UserController;
import com.hvtt.web.utilities.SocialAPIWrapper;
import com.hvtt.web.utilities.Utilities;
import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ComponentSystemEvent;
import org.primefaces.context.RequestContext;
import org.primefaces.model.menu.MenuItem;

/**
 *
 * @author Nguyễn Hoàng Danh
 */
@ManagedBean
@SessionScoped
public class MainController implements Serializable {

    private String includePage = "ui/default.xhtml";
    private String historyIncludePage = "";
    private String includeNewPage = "";
    private String includeViewPage = "";
    private String includeFilterMorePage = "";
    private String layoutName = "default";

    public void setLayoutName(String layoutName) {
        this.layoutName = layoutName;
    }

    public String getLayoutName() {
        return layoutName;
    }

    public void changePage(ActionEvent event) {
        MenuItem menuItem = (MenuItem) event.getSource();
        this.setIncludePage("ui/" + menuItem.getId() + ".xhtml");

        RequestContext.getCurrentInstance().update(Arrays.asList("contentForm"));
    }

    public void changeLayoutPane(ActionEvent event) throws SQLException {
        UtilsController utilsController = Utilities.getManagedBean(UtilsController.class);
        utilsController.unsupportMethod();
    }

    public String getIncludePage() {
        return includePage;
    }

    public void setIncludePage(String includePage) {
        this.setHistoryIncludePage(this.getIncludePage());
        this.includePage = includePage;
    }

    public void setHistoryIncludePage(String historyIncludePage) {
        this.historyIncludePage = historyIncludePage;
    }

    public String getHistoryIncludePage() {
        return historyIncludePage;
    }

    public String getIncludeNewPage() {
        return includeNewPage;
    }

    public void setIncludeNewPage(String includeNewPage) {
        this.includeNewPage = includeNewPage;
    }

    public String getIncludeViewPage() {
        return includeViewPage;
    }

    public void setIncludeViewPage(String includeViewPage) {
        this.includeViewPage = includeViewPage;
    }

    public String getIncludeFilterMorePage() {
        return includeFilterMorePage;
    }

    public void setIncludeFilterMorePage(String includeFilterMorePage) {
        this.includeFilterMorePage = includeFilterMorePage;
    }

    public void handleEvent(ComponentSystemEvent event) {
        Object object = FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        if (object == null) {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("login.html");
            } catch (IOException ex) {
                Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
            }
        }
        UserController userController = Utilities.getManagedBean(UserController.class);
        if (userController.getCurrentUser() == null) {
            Utilities.redirect("login.ht");
        }
    }

    public void doLogin(ActionEvent event) throws SQLException {
        UserController userController = Utilities.getManagedBean(UserController.class);
        userController.setCurrentUser(userController.getUser().checkLogin(userController.getUser().getUserID(), userController.getUser().getPassword()));

        if (userController.getCurrentUser() != null) {
            Utilities.redirect("index.ht");

        } else {
            UtilsController utilsController = Utilities.getManagedBean(UtilsController.class);
            RequestContext context = RequestContext.getCurrentInstance();
            utilsController.setMsgImage("/Administrator/images/warning.png");
            utilsController.setMessage(Utilities.getLanguages("AdMsg0004"));
            context.execute("PF('msgLogin').hide();PF('msgLoginErr').show();");
        }
    }

    public void doLogout(ActionEvent event) {
        /*UserController userController = Utilities.getManagedBean(UserController.class);
        userController.setCurrentUser(null);
        LanguagesController languagesController = Utilities.getManagedBean(LanguagesController.class);
        languagesController.setCurrentLanguages(null);
        this.setIncludePage("ui/default.xhtml");
        Utilities.redirect("login.ht");*/
    }

    public void changeTheme(ActionEvent event) throws SQLException {
        Utilities.getManagedBean(UtilsController.class).unsupportMethod();
    }

    public void changeLanguages(ActionEvent event) {
//        NgonnguDAO ngonnguDAO=(NgonnguDAO)Utilities.findBean(FacesContext.getCurrentInstance(),NgonnguDAO.class);
//        ngonnguDAO.setNgonnguChangeLanguages(ngonnguDAO.getNgonnguCtrl().findByKey(ManagementApplication.getCurrentInstance().getCurrentManagedSession(Utilities.getCurrentSession()).getLanguagesID()));
//        RequestContext.getCurrentInstance().execute("msg0006.show()");
    }
    public String dialogFacebookLink(){
        
        return SocialAPIWrapper.getDialogLink();
    }
}